
// #include "FastLED.h"  // 此示例程序需要使用FastLED库
// #include <SoftwareSerial.h>
// #include "DFRobotDFPlayerMini.h"
// #include "Arduino.h"
// #include "Button2.h"


// DFRobotDFPlayerMini myDFPlayer;
// SoftwareSerial mySerial(10, 11);  // RX, TX

// #define NUM_LEDS 67      // LED灯珠数量
// #define LED_DT 9         // Arduino输出控制信号引脚
// #define LED_TYPE WS2812  // LED灯带型号
// #define COLOR_ORDER GRB  // RGB灯珠中红色、绿色、蓝色LED的排列顺序

// const int buttonPin = 2;  // 按键Pin
// const int ledPin = 13;    // 指示灯Pin
// Button2 button = Button2(buttonPin);
// void handler(Button2& btn);

// uint8_t state_work = 0;       //开机状态
// uint8_t process_led_num = 0;  //led显示颜色
// uint8_t led_delay_num = 0;
// uint8_t process_dfplayer_num = 0;  //dfplayer线程状态
// uint8_t dfplayer_delay_num = 0;    //dfplayer延时

// uint8_t max_bright = 220;  // LED亮度控制变量，可使用数值为 0 ～ 255， 数值越大则光带亮度越高
// unsigned long lastDebounceTime = 0;
// unsigned long curDebounceTime = 0;
// uint8_t count = 0;

// CRGB leds[NUM_LEDS];  // 建立光带leds

// void setup() {

//   /*Serial Ini*/
//   Serial.begin(9600);

//   /*FastLed Ini*/
//   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带
//   FastLED.setBrightness(max_bright);                            // 设置光带亮度

//   /*Button Ini*/
//   pinMode(ledPin, OUTPUT);
//   button.setLongClickHandler(longClick);  // this will only be called upon release
//   button.setLongClickDetectedHandler(longClickDetected);

//   mySerial.begin(9600);  // MiniPlay 软件串口
//   Serial.println();
//   Serial.println(F("DFRobot DFPlayer Mini Demo"));
//   Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

//   delay(1000);
//   if (!myDFPlayer.begin(mySerial, /*isACK = */ true, /*doReset = */ true)) {  //Use serial to communicate with mp3.
//     Serial.println(F("Unable to begin:"));
//     Serial.println(F("1.Please recheck the connection!"));
//     Serial.println(F("2.Please insert the SD card!"));
//     while (true) {
//       delay(0);  // Code to compatible with ESP8266 watch dog.
//     }
//   }
//   Serial.println(F("DFPlayer Mini online."));
//   myDFPlayer.volume(25);  //Set volume value. From 0 to 30
//   // myDFPlayer.play(1);
//   // delay(2000);
//   // myDFPlayer.loop(2);
// }


// void loop() {
//   curDebounceTime = millis();
//   if (curDebounceTime - lastDebounceTime > 100)  //100ms进入一次循环
//   {
//     lastDebounceTime = curDebounceTime;
//     count++;
//     if (count == 5)
//       digitalWrite(ledPin, HIGH);
//     if (count == 10) {
//       count = 0;
//       digitalWrite(ledPin, LOW);
//     }
//     task_dfplayer_process();  //dfplayer 逻辑线程
//     task_led_process();       //fastled 逻辑线程
//                               //       fill_gradient(leds, 0, CHSV(50, 255,255) , 29, CHSV(150,255,255), LONGEST_HUES);
//     FastLED.show();

//     // if (myDFPlayer.available())
//     // {
//     //   printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
//     // }
//   }
//   button.loop();
// }


// void task_dfplayer_process() {
//   if (process_dfplayer_num == 1) {
//     dfplayer_delay_num = 0;
//     process_dfplayer_num = 3;
//     myDFPlayer.play(1);  //Play the first mp3
//   } else if (process_dfplayer_num == 2) {
//     process_dfplayer_num = 0;
//     myDFPlayer.pause();  //暂停播放
//   } else if (process_dfplayer_num == 3) {
//     dfplayer_delay_num++;  //第一首播放2秒
//     if (dfplayer_delay_num >= 20) {
//       process_dfplayer_num = 0;
//       myDFPlayer.loop(2);  //循环播放第二首MP3
//     }
//   }
// }

// /*函数名称：RGB刷新函数*/
// void task_led_process() {
//   if (process_led_num == 0 || led_delay_num > 0) {
//     if (led_delay_num > 0)
//       led_delay_num--;
//     //      fill_solid(leds,NUM_LEDS-1,CRGB::Black);
//     fill_gradient_RGB(leds, 0, CRGB::Black, NUM_LEDS - 1, CRGB::Black);
//   } else if (process_led_num == 1) {
//     fill_gradient_RGB(leds, 0, CRGB::White, NUM_LEDS - 1, CRGB::White);
//   } else if (process_led_num == 2) {
//     //      fill_solid(leds,NUM_LEDS-1,CRGB::Red);
//     fill_gradient_RGB(leds, 0, CRGB::Red, NUM_LEDS - 1, CRGB::Red);
//   } else if (process_led_num == 3) {
//     //      fill_solid(leds,NUM_LEDS-1,CRGB::Blue);
//     fill_gradient_RGB(leds, 0, CRGB::Blue, NUM_LEDS - 1, CRGB::Blue);
//   } else if (process_led_num == 4) {
//     //        fill_solid(leds,NUM_LEDS-1,CRGB::Blue);
//     fill_gradient_RGB(leds, 0, CRGB::Green, NUM_LEDS - 1, CRGB::Green);
//   } else if (process_led_num == 5) {
//     fill_gradient_RGB(leds, 0, CRGB::Red, NUM_LEDS - 1, CRGB::Blue);
//   }
// }



// void longClick(Button2& btn) {
//   unsigned int time = btn.wasPressedFor();
//   Serial.print("You clicked ");
//   if (time > 2000) {
//     if (state_work == 0) {
//       state_work = 1;
//       led_delay_num = 5;
//       process_led_num = 1;
//       process_dfplayer_num = 1;
//     } else {
//       state_work = 0;
//       process_led_num = 0;
//       process_dfplayer_num = 2;
//     }

//   } else {
//     if (state_work) {
//       process_dfplayer_num = 1;
//       led_delay_num = 5;
//       process_led_num++;
//       if (process_led_num > 5)
//         process_led_num = 1;
//     }
//   }
//   Serial.print(" (");
//   Serial.print(time);
//   Serial.println(" ms)");
// }


// void longClickDetected(Button2& btn) {
//   Serial.print("long click #");
//   Serial.print(btn.getLongClickCount());
//   Serial.println(" detected");
// }

// void printDetail(uint8_t type, int value) {
//   switch (type) {
//     case TimeOut:
//       Serial.println(F("Time Out!"));
//       break;
//     case WrongStack:
//       Serial.println(F("Stack Wrong!"));
//       break;
//     case DFPlayerCardInserted:
//       Serial.println(F("Card Inserted!"));
//       break;
//     case DFPlayerCardRemoved:
//       Serial.println(F("Card Removed!"));
//       break;
//     case DFPlayerCardOnline:
//       Serial.println(F("Card Online!"));
//       break;
//     case DFPlayerUSBInserted:
//       Serial.println("USB Inserted!");
//       break;
//     case DFPlayerUSBRemoved:
//       Serial.println("USB Removed!");
//       break;
//     case DFPlayerPlayFinished:
//       Serial.print(F("Number:"));
//       Serial.print(value);
//       Serial.println(F(" Play Finished!"));
//       break;
//     case DFPlayerError:
//       Serial.print(F("DFPlayerError:"));
//       switch (value) {
//         case Busy:
//           Serial.println(F("Card not found"));
//           break;
//         case Sleeping:
//           Serial.println(F("Sleeping"));
//           break;
//         case SerialWrongStack:
//           Serial.println(F("Get Wrong Stack"));
//           break;
//         case CheckSumNotMatch:
//           Serial.println(F("Check Sum Not Match"));
//           break;
//         case FileIndexOut:
//           Serial.println(F("File Index Out of Bound"));
//           break;
//         case FileMismatch:
//           Serial.println(F("Cannot Find File"));
//           break;
//         case Advertise:
//           Serial.println(F("In Advertise"));
//           break;
//         default:
//           break;
//       }
//       break;
//     default:
//       break;
//   }
// }

#include <Arduino.h>
#include <FastLED.h>  // 此示例程序需要使用FastLED库
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>  // Mini播放器控制库
#include <Button2.h>              // 按钮库
#include "async_time.h"           // 定时器

#define NUM_LEDS 5       // LED灯珠数量
#define LED_DT 9         // Arduino输出控制信号引脚
#define LED_TYPE WS2812  // LED灯带型号
#define COLOR_ORDER GRB  // RGB灯珠中红色、绿色、蓝色LED的排列顺序
#define BUTTON_PIN 2     // 按钮引脚

Button2 button;
boolean button_state = false;  // 按钮状态
uint8_t led_type_index = 0;    // LED的显示类型
int max_bright = 20;           // LED亮度控制变量，可使用数值为 0 ～ 255， 数值越大则光带亮度越高

CRGB leds[NUM_LEDS];  // 建立光带leds

CRGB red = CRGB::Red;        // 红色
CRGB orange = CRGB::Orange;  // 橙色
CRGB yellow = CRGB::Yellow;  // 黄色
CRGB green = CRGB::Green;    // 绿色
CRGB cyan = CRGB::Cyan;      // 青色
CRGB blue = CRGB::Blue;      // 蓝色
CRGB violet = CRGB::Violet;  // 紫色
CRGB colors_rgb[7] = { red, orange, yellow, green, cyan, blue, violet };
int interval = 20;
AsyncTime led_time_1 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_2 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_3 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_4 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_5 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_6 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_7 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_8 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_9 = AsyncTime(0, NUM_LEDS, 100);
AsyncTime led_time_10 = AsyncTime(0, NUM_LEDS, 100);
boolean is_led_1 = false;
boolean is_led_2 = false;
int led_index[NUM_LEDS];

unsigned long now = 0;
byte counter = 0;

void setup() {
  Serial.begin(9600);                                           // 设置波特率为 9600
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带
  FastLED.setBrightness(max_bright);                            // 设置光带亮度
  button.begin(BUTTON_PIN);                                     // 设置按钮引脚
  button.setLongClickHandler(longClick);
  button.setLongClickDetectedHandler(longClickDetected);
  button.setDoubleClickHandler(double_click_handler);  // 双击回调
  // button.setLongClickHandler(long_click_handler);      // 长按回调
  button.setLongClickTime(1000);  // 设置长按时间 单位毫秒

  for (int i = 0; i < NUM_LEDS; i++) {
    led_index[i] = 0;
  }
}

void loop() {
  task_led_process();
  button.loop();
  // delay(20);
}

void task_led_process() {
  Serial.print("led_type_index: ");
  Serial.println(led_type_index);
  if (led_type_index == 0) {
    fill_gradient_RGB(leds, NUM_LEDS, CRGB::Black, CRGB::Black);
    FastLED.show();
  }
  led_time_1.start(colorful_streamer, is_led_1);
  led_time_2.start(red_colorful_streamer, is_led_2);
}

void longClick(Button2 &btn) {
  unsigned int time = btn.wasPressedFor();
  Serial.print("You clicked ");
  if (time > 2000) {
    if (button_state) {
      button_state = false;
      led_type_index = 0;
    } else {
      button_state = true;
      led_type_index = 1;
    }
  } else {
    if (button_state) {
      led_type_index++;
      if (led_type_index > 2)
        led_type_index = 1;
    }
  }
  Serial.print(" (");
  Serial.print(time);
  Serial.println(" ms)");
}

void longClickDetected(Button2 &btn) {
  Serial.print("long click #");
  Serial.print(btn.getLongClickCount());
  Serial.println(" detected");
}

void click_handler(Button2 &btn) {
  Serial.println("单击");
  led_type_index++;
  if (led_type_index > 2) {
    led_type_index = 1;
  }
  is_led_1 = led_type_index == 1;
  is_led_2 = led_type_index == 2;
}

void double_click_handler(Button2 &btn) {
  Serial.println("双击");
  max_bright = max_bright - 10;
  if (max_bright < 10) {
    max_bright = 10;
  }
  FastLED.setBrightness(max_bright);
}

void long_click_handler(Button2 &btn) {
  Serial.println("长按");
  max_bright = max_bright + 10;
  if (max_bright > 255) {
    max_bright = 10;
  }
  FastLED.setBrightness(max_bright);
}

// 七彩流光
void colorful_streamer(int i) {
  led_index[i]++;
  if (led_index[i] == 7) {
    led_index[i] = 0;
  }
  leds[i] = colors_rgb[led_index[i]];
  FastLED.show();
  led_time_1.clearPreviousTime();
}

// 红蓝流光
void red_colorful_streamer(int i) {
  leds[i] = i % 2 == 0 ? red : blue;
  FastLED.show();
  led_time_2.clearPreviousTime();
}